<?

/**
 * DevSuite
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://suite.rapidcodefactory.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@rapidcodefactory.com so we can send you a copy immediately.
 *
 * @category   RFC_Model
 * @package    RCF_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
 * @version    
*/



/**
 * Class for DB_Object Model
 *
 * @category   RFC_Model
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/

require_once('Comments.php');
require_once('Users.php');
class Discussions extends DomainModel {
	protected $id;	//primaryKey=yes, dataType=int, maxLength=, allowNulls=no
	protected $appsId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=no
	protected $usersId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=no
	protected $recipientId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=no
	protected $statusesId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=no
	protected $addDate;	//primaryKey=no, dataType=datetime, maxLength=, allowNulls=no
	protected $title;	//primaryKey=no, dataType=varchar, maxLength=100, allowNulls=yes
	protected $content;	//primaryKey=no, dataType=text, maxLength=65535, allowNulls=yes
	protected $lastCommentId;
	protected $firstCommentId;
	
	protected $lastComment;
	protected $firstComment;
	protected $comments;
	protected $author;
	protected $recipient;
	
	protected $config;
	
	public function getConfig(){
		if (is_null($this->config)){
			$registry = Zend_Registry::getInstance();
			$config = $registry->get('config');
			$this->config = $config;
		}
		
		return $this->config;
	}
	
	function getLastComment(){
		if (is_null($this->lastComment)){
			$this->lastComment = ModelMapper::instance('Comments')->find($this->lastCommentId);
		}
		
		return $this->lastComment;
	}
	
	function getFirstComment(){
		if (is_null($this->firstComment)){
			$this->firstComment = ModelMapper::instance('Comments')->find($this->firstCommentId);
		}
		
		return $this->firstComment;
	}
	
	function getAuthor(){
		if (is_null($this->author)){
			$this->author = ModelMapper::instance('Users')->find($this->usersId);
		}
		
		return $this->author;
	}
	
	function getRecipient(){
		if (is_null($this->recipient)){
			$this->recipient = ModelMapper::instance('Users')->find($this->recipientId);
		}
		
		return $this->recipient;
	}
	
	function getComments(){
		if (is_null($this->comments)){
			$filter = "WHERE object_id = ".$this->id." AND content_types_id = ".$this->getConfig()->contentTypes->discussionEntry;
			$orderBy = "ORDER BY id ASC";
			$this->comments = ModelMapper::instance('Comments')->findFiltered($filter, $orderBy);
		}
		
		return $this->comments;
	}
}




/**
 * Class for DB_Object ModelMapper
 *
 * @category   RFC_Model
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class DiscussionsMapper extends ModelMapper {
	function __construct(){
		parent::__construct();
		$this->tableName = 'discussions';
		$this->selectViewName = 'v_discussions';
		$this->selectFields = '
				id,
				apps_id,
				users_id,
				recipient_id,
				statuses_id,
				add_date,
				title,
				content,
				first_comment_id,
				last_comment_id,
				u_id,
				u_apps_id,
				u_statuses_id,
				u_username,
				u_first_name,
				u_last_name,
				u_email,
				u_is_operator,
				u_is_administrator,
				u_remarks,
				u_main_image_id,
			    ur_id,
				ur_apps_id,
				ur_statuses_id,
				ur_username,
				ur_first_name,
				ur_last_name,
				ur_email,
				ur_is_operator,
				ur_is_administrator,
				ur_remarks,
				ur_main_image_id,
				cm_id,
    			cm_content_types_id,
    			cm_object_id,
    			cm_users_id,
    			cm_add_date,
    			cm_content,
    			cm_xml_data,
    			fcm_id,
    			fcm_content_types_id,
    			fcm_object_id,
    			fcm_users_id,
    			fcm_add_date,
    			fcm_content,
    			fcm_xml_data
		';

		$this->findQuery = self::$DB->prepare("
			SELECT ".$this->selectFields."
			FROM ".$this->selectViewName."
			WHERE id = ?
		");
		$this->insertQuery = self::$DB->prepare("
			CALL p_add_discussions (@p_id,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?
		)");


		$this->updateQuery = self::$DB->prepare("
			CALL p_upd_discussions (
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?

		)");

		$this->deleteQuery = self::$DB->prepare("
			CALL p_del_discussions (?)
		");

		$this->cacheTime = null;
	}

	protected function doLoad($array){
		$acuire = str_replace('Mapper', '', get_class($this));
		$obj = new $acuire($array['id']);
		$obj->setId($array['id']);
		$obj->setAppsId($array['apps_id']);
		$obj->setUsersId($array['users_id']);
		$obj->setRecipientId($array['recipient_id']);
		$obj->setStatusesId($array['statuses_id']);
		$obj->setAddDate($array['add_date']);
		$obj->setTitle($array['title']);
		$obj->setContent($array['content']);
		$obj->setLastCommentId($array['last_comment_id']);
		$obj->setFirstCommentId($array['first_comment_id']);

		$arr = ArrayUtils::splitByPrefix($array, array('u_', 'ur_', 'cm_', 'fcm_'));
		if (!empty($arr['u_']))
			ModelMapper::instance('Users')->loadArray($arr['u_']);
		if (!empty($arr['ur_']))
			ModelMapper::instance('Users')->loadArray($arr['ur_']);
		if (!empty($arr['cm_']))
			ModelMapper::instance('Comments')->loadArray($arr['cm_']);
		if (!empty($arr['fcm_']))
			ModelMapper::instance('Comments')->loadArray($arr['fcm_']);
			
		return $obj;
	}

	function insert(DomainModel $obj){

		$values = array(
			$obj->getAppsId(),
			$obj->getUsersId(),
			$obj->getRecipientId(),
			$obj->getStatusesId(),
			$obj->getAddDate(),
			$obj->getTitle(),
			$obj->getContent(),
			$obj->getLastCommentId(),
			$obj->getFirstCommentId()
		);

		$this->doStatement($this->insertQuery, $values);
		$obj->setId(self::$DB->getOne("SELECT @p_id"));
	}

	function update(DomainModel $obj){
		$values = array(
			$obj->getId(),
			$obj->getAppsId(),
			$obj->getUsersId(),
			$obj->getRecipientId(),
			$obj->getStatusesId(),
			$obj->getAddDate(),
			$obj->getTitle(),
			$obj->getContent(),
			$obj->getLastCommentId(),
			$obj->getFirstCommentId()
		);

		$this->doStatement($this->updateQuery, $values);
	}

	function delete(DomainModel $obj){
		$this->doStatement($this->deleteQuery, array($obj->getId()));
	}

	function newId(){

		$row = self::$DB->getRow('SHOW TABLE STATUS LIKE discussions');
		return $row['Auto_increment'];

	}
}


/**
 * Class for DB_Object Model
 *
 * @category   RFC_ModelCollection
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class DiscussionsCollection extends ModelCollection {
	function add(Discussions $model){
		$this->doAdd($model);
	}
}


/**
 * Class for DB_Object Model
 *
 * @category   RFC_ModelCollectionExtended
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class DiscussionsCollectionExtended extends DiscussionsCollection {
	private $stmt;
	private $valueArray;
	private $mapper;
	private $run = false;

	function __construct(ModelMapper $mapper, $stmtHandle, $valueArray){
		parent::__construct();
		$this->stmt = $stmtHandle;
		$this->valueArray = $valueArray;
		$this->mapper = $mapper;
	}

	function notifyAccess(){
		if (!$this->run){
			$result = $this->mapper->doStatement($this->stmt, $this->valueArray,$this->mapper->getCacheTime());
			$this->init_db($result, $this->mapper);
			$this->run = true;
		}
	}
}

?>
